package servlet;
import util.MyUtil;

import java.io.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import javax.servlet.*;
@WebServlet("/downloadServlet")
public class DownloadServlet extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        File downLoadFileDir=new File(getServletContext().getRealPath("/uploadFiles"));
        String aFileName=null;
        FileInputStream in=null;
        ServletOutputStream out=null;
        try {
            aFileName=request.getParameter("resPath");
            response.setHeader("Content-Type","application/x-msdownload");
            response.setHeader("Content-Disposition","attachment;filename="+ MyUtil.toUTF8String(aFileName));
            out=response.getOutputStream();
            out.flush();
            int aRead=0;
            byte b[]=new byte[1024];
            while((aRead=in.read(b))!=-1&in!=null){
                out.write(b,0,aRead);
            }
            out.flush();
            in.close();
            out.close();
        }catch (Throwable e){
            e.printStackTrace();
        }
    }
}
